package request

import (
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"lab.com/consts"
	"lab.com/define/errorcode"
)

// BannerBasicInfo 轮播图列表信息
type BannerBasicInfo struct {
	Id       int    `json:"id"`
	ImgPath  string `json:"img_path"`
	SkipLink string `json:"skip_link"`
}

// BannerListRes 轮播图列表返回结果
type BannerListRes struct {
	List  []BannerBasicInfo `json:"list"`
	Count int               `json:"count"`
}

// BannerAddReq 轮播图新增参数
type BannerAddReq struct {
	Title    string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	IsShow   int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	SkipLink string `json:"skip_link" form:"skip_link" validate:"required,max=255" comment:"跳转链接"`
	ImgPath  string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
	Sort     int    `json:"sort" form:"sort" validate:"required" comment:"排序，越大越前"`
}

// CheckInputParams return input params
func (banner *BannerAddReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(banner); err != nil {
		return err
	}
	if errs, err := Validate(banner); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// BannerListInManageReq 轮播图管理列表查询参数
type BannerListInManageReq struct {
	PageOffset
}

// BannerInfo 轮播图列表信息
type BannerInfo struct {
	Id       int    `json:"id"`
	Title    string `json:"title"`
	SkipLink string `json:"skip_link"`
	ImgPath  string `json:"img_path"`
	IsShow   int    `json:"is_show"`
	Sort     int    `json:"sort"`
}

// BannerListInManageRes 轮播图列表返回结果
type BannerListInManageRes struct {
	List  []BannerInfo `json:"list"`
	Count int          `json:"count"`
}

// CheckInputParams return input params
func (banner *BannerListInManageReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(banner); err != nil {
		return err
	}
	if errs, err := Validate(banner); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}

	banner.ParseOffset()
	return nil
}

// BannerEditReq 轮播图修改参数
type BannerEditReq struct {
	ID       int    `json:"id" form:"id" validate:"required" comment:"轮播图id"`
	Title    string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	IsShow   int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	SkipLink string `json:"skip_link" form:"skip_link" validate:"required,max=255" comment:"跳转链接"`
	ImgPath  string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
	Sort     int    `json:"sort" form:"sort" validate:"required" comment:"排序，越大越前"`
}

// CheckInputParams return input params
func (banner *BannerEditReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(banner); err != nil {
		return err
	}
	if errs, err := Validate(banner); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// BannerDeleteReq 轮播图删除参数
type BannerDeleteReq struct {
	ID       int    `json:"id" form:"id" validate:"required" comment:"轮播图id"`
}

// CheckInputParams return input params
func (banner *BannerDeleteReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(banner); err != nil {
		return err
	}
	if errs, err := Validate(banner); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}


// BannerShowReq 轮播图显示参数
type BannerShowReq struct {
	ID       int    `json:"id" form:"id" validate:"required" comment:"轮播图id"`
	IsShow   int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
}

// CheckInputParams return input params
func (banner *BannerShowReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(banner); err != nil {
		return err
	}
	if errs, err := Validate(banner); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}